var getdata = null;
var statsData = false;
var indexloading = null;
var clickid=[];
$(function(){
    var form = layui.form;
    refreshs()
    $.ajax({
        type : "get",
        url : "platform/findAllPlatform",
        dataType : "json",
        success: function(data) {

            if(data.code==0){
                var _html = ''
                for(var i=0;i<data.platformList.length;i++){
                    _html = _html + '<div type="button" data-id="'+data.platformList[i].platformId+'" class="btn btn-default">'+data.platformList[i].platformName+'</div>'
                    clickid.push(data.platformList[i].platformId);
                }

                $('#dateselect').append(_html);
                var color =['#2d8cf0','#19be6b', '#ff9900', '#e46cbb', '#9a66e4','#ed3f14','#CC9900','#9999FF','#996600','#009900','#336666','#666600','#9999CC','#CCCC33']
                $('#dateselect .btn').each(function(index){
                    $(this).css({
                        'background':color[index],
                        'color':'#ffffff'
                    })

                    $(this).click(function(){
                        if(statsData == false){
                            if($(this).css('backgroundColor')!='rgb(204, 204, 204)'){
                                $(this).css({
                                    'background':'#cccccc',
                                    'color':'#ffffff'
                                })
                                for(var i=0;i<clickid.length;i++){
                                    if(clickid[i]==$(this).attr('data-id')){
                                        clickid.splice(i,1)
                                    }
                                }
                            }else{
                                $(this).css({
                                    'background':color[index],
                                    'color':'#ffffff'
                                })
                                clickid.push($(this).attr('data-id'))
                            }
                            console.log(clickid,'ssssssssssssss')
                            refreshs()
                        }

                    })
                });


            }else{
                layer.msg('数据获取失败');
            }
        },
        error:function(){
            statsData = false;
            layer.msg('数据获取失败');
        }
    });
    $('.arrows').click(function(){
        if($('#refreshs').css('right')=='0px'){
            $('.arrows span').removeClass('glyphicon-arrow-right').addClass('glyphicon-arrow-left');
            $('#refreshs').animate({
                right:'-150px'
            })
        }else{
            $('.arrows span').removeClass('glyphicon-arrow-left').addClass('glyphicon-arrow-right');
            $('#refreshs').animate({
                right:'0px'
            })
        }
    })
})


function refreshs(){

    indexloading = layer.load(2);
    statsInit(clickid);
    chartPage.init(clickid);
    window.clearInterval(getdata);
    getdata = window.setInterval(function(){
        indexloading = layer.load(2);
        statsInit(clickid);
        chartPage.init(clickid);
    },1000*60)
}

function statsInit(id){
    if(!statsData){
        statsData = true;
        $.ajax({
            type : "POST",
            url : "statis/getStatis",
            cache : false,
            contentType : false,
            processData : false,
            dataType : "json",
            success: function(data) {
                statsData = false;
                layer.close(indexloading);
                if(data.code==0){
                    setData(data)
                }else{
                    layer.msg('数据获取失败');
                }
            },
            error:function(){
                statsData = false;
                layer.msg('数据获取失败');
            }
        });
        $.ajax({
            type : "POST",
            url : "ip/getIndexIpData",
            cache : false,
            dataType : "json",
            success: function(data) {
                if(data.code==0){
                    //ip信息

                    var demo = new CountUp('stats5_num', 0, transformNum(data.ipCount.ipCount),isInteger(transformNum(data.ipCount.ipCount))?0:1, 2, {
                        useEasing: true,
                        useGrouping: true,
                        separator: ',',
                        decimal: '.',
                        suffix: String(data.ipCount.ipCount.length)>=5?'万':''
                    });
                    demo.start();

                    if(data.ipCount.riseOrFall==1){

                        $('#stats5 .stats_contrast_num').html('+'+data.ipCount.ratio+'%').css({'color':'red'});
                        $('#stats5 .glyphicon').removeClass('glyphicon-arrow-down ').addClass('glyphicon-arrow-up ')
                    }else{

                        $('#stats5 .stats_contrast_num').html(data.ipCount.ratio+'%').css({'color':'#76d471'});
                        $('#stats5 .glyphicon').removeClass('glyphicon-arrow-up ').addClass('glyphicon-arrow-down ')
                    }
                }else{
                    layer.msg('数据获取失败');
                }
            },
            error:function(){
                statsData = false;
                layer.msg('数据获取失败');
            }
        });
    }

}
function isInteger(obj) {

    return Math.round(obj) == obj   //是整数，则返回true，否则返回false
}


function setData(data){
    //累计用户数
    var demo = new CountUp('stats1_num', 0, transformNum(data.result.totalUser),isInteger(transformNum(data.result.totalUser))?0:1, 2, {
        useEasing: true,
        useGrouping: true,
        separator: ',',
        decimal: '.',
        suffix: String(data.result.totalUser.length)>=5?'万':''
    });
    demo.start();

    //新增用户数
    var demo = new CountUp('stats2_num', 0, transformNum(data.result.newUser),isInteger(transformNum(data.result.newUser))?0:1, 2, {
        useEasing: true,
        useGrouping: true,
        separator: ',',
        decimal: '.',
        suffix: String(data.result.newUser.length)>=5?'万':''
    });
    demo.start();

    if(data.result.newUserRiseOrFall==1){

        $('#stats2 .stats_contrast_num').html('+'+data.result.newUserPercent+'%').css({'color':'red'});
        $('#stats2 .glyphicon').removeClass('glyphicon-arrow-down ').addClass('glyphicon-arrow-up ')
    }else{

        $('#stats2 .stats_contrast_num').html(data.result.newUserPercent+'%').css({'color':'#76d471'});
        $('#stats2 .glyphicon').removeClass('glyphicon-arrow-up ').addClass('glyphicon-arrow-down ')
    }

    //活跃用户数
    var demo = new CountUp('stats3_num', 0, transformNum(data.result.activeUser),isInteger(transformNum(data.result.activeUser))?0:1, 2, {
        useEasing: true,
        useGrouping: true,
        separator: ',',
        decimal: '.',
        suffix: String(data.result.activeUser.length)>=5?'万':''
    });
    demo.start();

    if(data.result.activeUserRiseOrFall==1){
        $('#stats3 .stats_contrast_num').html('+'+data.result.activeUserPercent+'%').css({'color':'red'});
        $('#stats3 .glyphicon').removeClass('glyphicon-arrow-down ').addClass('glyphicon-arrow-up ')
    }else{
        $('#stats3 .stats_contrast_num').html(data.result.activeUserPercent+'%').css({'color':'#76d471'});
        $('#stats3 .glyphicon').removeClass('glyphicon-arrow-up ').addClass('glyphicon-arrow-down ')
    }

    //转发文章数
    var demo = new CountUp('stats4_num', 0, transformNum(data.result.forwardArticle),isInteger(transformNum(data.result.forwardArticle))?0:1, 2, {
        useEasing: true,
        useGrouping: true,
        separator: ',',
        decimal: '.',
        suffix: String(data.result.forwardArticle.length)>=5?'万':''
    });
    demo.start();

    if(data.result.forwardArticleRiseOrFall==1){

        $('#stats4 .stats_contrast_num').html('+'+data.result.forwardArticlePercent+'%').css({'color':'red'});
        $('#stats4 .glyphicon').removeClass('glyphicon-arrow-down ').addClass('glyphicon-arrow-up ')
    }else{

        $('#stats4 .stats_contrast_num').html(data.result.forwardArticlePercent+'%').css({'color':'#76d471'});
        $('#stats4 .glyphicon').removeClass('glyphicon-arrow-up ').addClass('glyphicon-arrow-down ')
    }


    //文章新增数
    var demo = new CountUp('stats6_num', 0, transformNum(data.result.newArticle),isInteger(transformNum(data.result.newArticle))?0:1, 2, {
        useEasing: true,
        useGrouping: true,
        separator: ',',
        decimal: '.',
        suffix: String(data.result.newArticle.length)>=5?'万':''
    });
    demo.start();
    $('#stats6 .stats_contrast_num').html(data.result.newArticlePercent+'%');
    if(data.result.newArticleRiseOrFall==1){

        $('#stats6 .stats_contrast_num').html('+'+data.result.newArticlePercent+'%').css({'color':'red'})
        $('#stats6 .glyphicon').removeClass('glyphicon-arrow-down ').addClass('glyphicon-arrow-up ')
    }else{

        $('#stats6 .stats_contrast_num').html(data.result.newArticlePercent+'%').css({'color':'#76d471'})
        $('#stats6 .glyphicon').removeClass('glyphicon-arrow-up ').addClass('glyphicon-arrow-down ')
    }
    //被封域名数
    var demo = new CountUp('stats7_num', 0, transformNum(data.result.lockedDomain),isInteger(transformNum(data.result.lockedDomain))?0:1, 2, {
        useEasing: true,
        useGrouping: true,
        separator: ',',
        decimal: '.',
        suffix: String(data.result.lockedDomain.length)>=5?'万':''
    });
    demo.start();

    if(data.result.lockedDomainRiseOrFall==1){
        $('#stats7 .stats_contrast_num').html('+'+data.result.lockedDomainPercent+'%').css({'color':'red'})
        $('#stats7 .glyphicon').removeClass('glyphicon-arrow-down ').addClass('glyphicon-arrow-up ')
    }else{
        $('#stats7 .stats_contrast_num').html(data.result.lockedDomainPercent+'%').css({'color':'#76d471'});
        $('#stats7 .glyphicon').removeClass('glyphicon-arrow-up ').addClass('glyphicon-arrow-down ')
    }
}
function transformNum(e,b){
    if(String(e).length<=4){
        return String(e);
    }else{
        var _e
        if(((parseFloat(e)/10000).toString()).indexOf('.')!=-1){
            _e = (parseFloat(e)/10000).toFixed(1);
        }else{
            _e = (parseFloat(e)/10000)
        }
        return _e+ (b==1?'万':'');
    }
}